package cn.toma.service.impl.sys;

import cn.toma.common.vo.StringPool;
import cn.toma.core.AbstractService;
import cn.toma.dao.sys.RoleMapper;
import cn.toma.dao.sys.RoleMenuMapper;
import cn.toma.manager.sys.UserManager;
import cn.toma.model.sys.Role;
import cn.toma.model.sys.RoleMenu;
import cn.toma.service.sys.RoleMenuServie;
import cn.toma.service.sys.RoleService;
import cn.toma.service.sys.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("roleService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RoleServiceImpl extends AbstractService<Role> implements RoleService
{


	@Resource
	private RoleMapper roleMapper;

	@Resource
	private RoleMenuMapper roleMenuMapper;
	@Resource
	private UserRoleService userRoleService;
	@Autowired
	private RoleMenuServie roleMenuService;
	@Autowired
	private UserManager userManager;

	@Override
	public List<Role> findRolePage(Role role)
	{
		return this.roleMapper.findRolePage(role);
	}

	@Override
	public List<Role> findRoles(Role role)
	{
		try {
			Weekend<Role> weekend = new Weekend<>(Role.class);
			WeekendCriteria<Role, Object> weekendCriteria = weekend.weekendCriteria();


			if (StringUtils.isNotBlank(role.getRoleName())) {
				weekendCriteria.andEqualTo(Role::getRoleName, role.getRoleName());
			}
			if (StringUtils.isNotBlank(role.getCreateTimeFrom()) && StringUtils.isNotBlank(role.getCreateTimeTo())) {
				weekendCriteria.andGreaterThanOrEqualTo
						(Role::getCreateTime, role.getCreateTimeFrom())
						.andLessThanOrEqualTo(Role::getCreateTime, role.getCreateTimeTo());
			}
			return this.roleMapper.selectByCondition(weekend);
		} catch (Exception e) {
			log.error("获取角色信息失败", e);
			return null;
		}
	}

	@Override
	public List<Role> findUserRole(String userName)
	{
		return roleMapper.findUserRole(userName);
	}

	@Override
	public Role findByName(String roleName)
	{
		Role role = new Role();
		role.setRoleName(roleName);
		return this.roleMapper.selectOne(role);
	}

	@Override
	public void createRole(Role role)
	{
		role.setCreateTime(new Date());
		this.save(role);

		String[] menuIds = role.getMenuIds().split(StringPool.COMMA);
		setRoleMenus(role, menuIds);
	}

	@Override
	public void deleteRoles(String[] roleIds) throws Exception
	{

		List<String> list = Arrays.asList(roleIds);
		list.forEach(item ->
		{
			this.roleMapper.deleteByPrimaryKey(item);
		});


		this.roleMenuService.deleteRoleMenusByRoleId(roleIds);
		this.userRoleService.deleteUserRolesByRoleId(roleIds);


	}

	@Override
	public void updateRole(Role role) throws Exception
	{

		role.setModifyTime(new Date());
		roleMapper.updateByPrimaryKeySelective(role);

		Weekend<RoleMenu> weekend = new Weekend<>(RoleMenu.class);
		WeekendCriteria<RoleMenu, Object> weekendCriteria = weekend.weekendCriteria();
		weekendCriteria.andEqualTo(RoleMenu::getRoleId, role.getRoleId());
		roleMenuMapper.deleteByCondition(weekend);

		String[] menuIds = role.getMenuIds().split(StringPool.COMMA);
		setRoleMenus(role, menuIds);

	}

	private void setRoleMenus(Role role, String[] menuIds)
	{
		Arrays.stream(menuIds).forEach(menuId ->
		{
			RoleMenu rm = new RoleMenu();
			rm.setMenuId(Long.valueOf(menuId));
			rm.setRoleId(role.getRoleId());
			this.roleMenuMapper.insert(rm);
		});
	}
}
